var form = layui.form,
    laydate = layui.laydate,
    upload = layui.upload,
    table = layui.table,
    $ = layui.jquery,
    layer = layui.layer,
    element = layui.element,
    messageId=getQueryVariable("messageId")
;

//附件集合
let annexData = [];
initMessage(messageId);
//查询消息详情
function initMessage(id){
    $.ajax({
        data:{"id":id},
        url:"message/queryByid",
        dataType:"json",
        async:false,
        success : function (da) {
            if(da.status==200 ){
                let res=da.data;
                $("#theme")[0].innerHTML=res.theme;
                $("#publisherName")[0].innerHTML=res.publisherName;
                $("#content")[0].innerHTML=res.content;
                annexData=res.annexList;
                console.log(annexData);
            }
        }
    });
}




//---附件
table.render({
    elem: '#attachment',
    data:annexData,
    cols: [[
        {field: 'numbers',title:'序号',type:'numbers'},
        {field: 'fileName', title: '文件名', sort: true,width: 600, event: "fileName"},
        {field: 'uploadTime', title: '上传日期',width: 600},
        {fixed: 'right', width: 80, toolbar: '#attachmenteditor', title: '操作'}
    ]]
    , id: 'attachmenttableid'
    , height: 500
    ,page: false
    ,limit: Number.MAX_VALUE
});

table.on('tool(attachmenttable)', function (obj) {
    let data = obj.data;
    if (obj.event === 'download') { //下载
        let xmlHttpRequest = new XMLHttpRequest();
        let url = "wjinfo/downloadfile2?filename=" + data.filePath;
        xmlHttpRequest.open('GET', url, true);
        //设置请求头
        xmlHttpRequest.setRequestHeader("Content-type", "application/json")
        xmlHttpRequest.responseType = "blob";
        xmlHttpRequest.send(null);

        xmlHttpRequest.onload = function() {
            let content = xmlHttpRequest.response;
            // 组装a标签
            let elink = document.createElement("a");

            //设置文件下载路径
            elink.download = decodeURIComponent(data.fileName);
            elink.style.display = "none";
            let blob = new Blob([content], { type: 'application/force-download' });

            //解决下载不存在文件的问题，根据blob大小判断
            if(blob.size==0){
                layer.msg('服务器没找到此文件，请联系管理员!');
            }else{
                elink.href = window.URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);
            }

        }
    }else if(obj.event === 'fileName'){//点击名称，在线预览
        onLineFile(obj.data.fileUrl);
    }
});




$("#shutdown").on('click',function () {
    parent.detab("views/jsp/BasicData/tpl/viewAnnouncementMessage.jsp?messageId="+messageId);
})
